package com.sca.common.excel.module.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: denglei
 * @Date: 2022/3/15 23:29
 * @Description:
 */
public class GenderConverter implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }


    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        String cellStr = context.getReadCellData().getStringValue();

        if(StringUtils.isEmpty(cellStr)){return null;}
        if("男".equals(cellStr)){
            return 0;
        }else if("女".equals(cellStr)){
            return 1;
        }else {
            return null;
        }
    }



    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        Integer value = context.getValue();
        if(value==null){
            return new WriteCellData<>("");
        }
        if(value==0){
            return new WriteCellData<>("男");
        }else if(value==1){
            return new WriteCellData<>("女");
        }else {
            return null;
        }
    }
}
